<?php
namespace Admin\Controller;
use Think\Controller;
class RootController extends CommonController {
	
	//显示用户列表
    public function index(){
		//查询所有用户 
		$auth_group=M('auth_group');
		//查询个数
        $count=$auth_group->count();
        //每页显示5个
        $page = new \Think\Page($count,5);
        //设置页码显示
        $page->setConfig('header', '共 %TOTAL_ROW% 条记录');
        $page->setConfig('first', '首页');
        $page->setConfig('last', '末页');
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('theme',"共 %TOTAL_ROW% 条记录 %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $this->assign('show', $page->show());
        // 2.查询数据
        $data = $auth_group->page(I('get.p',1), 5)->select();
        // 3.分配数据
        $this->assign('data', $data);
        $this->display();
    }

    //添加用户组
    public function add(){
    	$rule=M('auth_rule');
        $data= $rule->select();
    	$this -> assign("data",$data);
        $this -> display();
    }
    public function insert(){
        $data["title"] = $_POST['title'];

        $data["rules"] = implode(',',$_POST['id']) ;

        $auth_group = M('auth_group');
        if($auth_group -> create($data)){
            if($auth_group -> add()){
                $this -> success("添加成功！",U('Root/index'));
            }else{
                $this -> error("添加失败！",U('Root/index'));
            }
        }
    }

    //删除用户组
    public function del(){
        $id = I('id');
        $role = M('auth_group');
        if($role -> delete($id)){
            $this -> success("删除成功!",U('Root/index'));
        }else{
            $this -> error("删除失败！",U('Root/index'));
        }
    }

    //权限详情
    public function info(){
        $rules=I('get.rules');
        $rules = explode(",",$rules);
        $len = count($rules);
        $power = M('auth_rule');
        for($i=0 ;$i < $len ;$i++){
            $map['id'] = array('EQ',$rules[$i]);
            $data[$i] = $power -> where($map) -> find();
        }
        $this->assign('data', $data);
        $this->display();
    }
	



}
